#! /bin/sh

#   wget_arch downloads and archives files using wget
#   Copyright (C) 2012  Mitch Crane

#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.


# crontab example to run daily at 5:45 PM
# 45      17      *       *       *       /usr/local/sbin/wget-arch ~/archive_dir http://somesite.com/foo.pdf
#
# shouldn't be run more than once a day unless the file renaming string is
# changed. the URL is mandatory. if the directory is ommitied then the current
# directory is used. requires wget, awk and sed

# save the command name for logs
CMD=`echo "$0" | sed -r 's/.*\///'`;

# check params and set defaults
if [ $# = 1 ] ; then
   D="./"
   URL=$1
else
   if [ $# = 2 ] ; then
      D=$1
      URL=$2
   else
      echo "usage: $0 [path] http://blah.com/file.ext" >&2
      logger "$CMD error: $0 usage - check params"
   fi
fi

# fixup trailing slash or lack thereof
D=`echo "$D"  | sed -r 's/\/*$/\//'`;

# get the filename minus path and http junk
F=`echo "$URL"  | sed -r 's/.*\///'`;

# make a filename based on todays date and set vars for other files new file
# name. only uses year, month and day. add minutes if you will be checking more
# than once/day. ex: date +"%y-%m-%d-%M_$F"
NFN="$D"`date +"%y-%m-%d_$F"`;

# old file name
OFN="$D$F"

# hash file name
HFN="$D$F.md5"

if [ ! -d "$D" ]; then
   mkdir "$D"
   if [ "$?" != 0 ]; then
      logger "$CMD error: couldn't create directory $D"
      exit 0
   fi
   logger "$CMD status: created directory $D"
fi

wget --quiet --directory-prefix="$D" "$URL"

# if wget fails log and exit
ER="$?"
if [ "$ER" != 0 ]; then
   logger "$CMD error: $ER: wget failed to get $URL"
   exit "$ER"
fi

if [ ! -e "$OFN" ] ; then
   logger "$CMD error: $F not found"
   exit 1
fi

# test to see if we have .md5 from a previously downloaded file. if so then
# check the new file's hash against the last one
if [ -e "$HFN" ] ; then

   # md5sum the new file and set vars
   A=`md5sum -b "$OFN" | awk '{print $1}'`;
   B=`cat "$HFN"`;

   # if the sums match then delete the file
   if [ "$A" = "$B" ]; then
      logger "$CMD status: $F unchanged"
      rm "$OFN"
      exit 0
   fi
fi

# if we didn't have a .md5 or the files didn't match then we end up here. copy,
# make the .md5 file, log and leave
mv "$OFN" "$NFN"
md5sum -b "$NFN" | awk '{print $1}' > "$HFN"
logger "$CMD status: $OFN saved as $NFN"

exit 0
